package tgc.edu.demo.jkxt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.demo.jkxt.entity.SysUser;
import tgc.edu.demo.jkxt.security.UserUtils;
import tgc.edu.demo.jkxt.service.SysUserService;

@Controller
@RequestMapping(value = "/password")
public class PasswordController {

	@Autowired
	private SysUserService sysuserService;
	@Autowired
	private UserUtils userutils;
	
	@RequestMapping(value = "/manage")
	public void manage() {
		
	}
	
	@RequestMapping(value = "/edit")
	public String edit() {
		return "password/edit";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public boolean save(String password1,String password3) {
		SysUser user=userutils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		String pwd=user.getPassword();
		if(encoder.matches(password1, pwd)) {
			String password=encoder.encode(password3);
			user.setPassword(password);
			sysuserService.save(user);
			return true;
		}else {
			return false ;
		}
		
	}
}
